<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" th:href="@{/pages/favicon.ico}" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" th:href="@{/pages/css/font.css}" href="./css/font.css">
    <link rel="stylesheet" th:href="@{/pages/css/xadmin.css}" href="./css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="./lib/layui/layui.js" th:src="@{/pages/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/pages/js/xadmin.js}" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
            <input type="hidden" name="id" th:value="${article?.id}"/>
          <div class="layui-form-item">
              <label for="title" class="layui-form-label">
                  <span class="x-red">*</span>标题
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="title" name="title"  th:value="${article.title}" lay-verify="required" class="layui-input"/>
              </div>
          </div>
            <div class="layui-form-item">
                <label for="type" class="layui-form-label">
                    <span class="x-red">*</span>类别
                </label>
                <div class="layui-input-inline">
                    <select id="type" name="type" lay-verify="required">
                        <option th:value="${articleType.code}" th:each="articleType:${articleTypes}" th:checked="${#strings.equals(articleType.code,article.type)}" th:text="${articleType.name}"></option>
                    </select>
                </div>
            </div>
          <div class="layui-form-item">
              <label for="source" class="layui-form-label">
                  <span class="x-red">*</span>来源
              </label>
              <div class="layui-input-inline">
                  <input type="text" th:value="${article.source}" name="source" id="source"  lay-verify="required" class="layui-input"/>
              </div>
          </div>
            <div class="layui-form-item">
                <label for="uploadPath" class="layui-form-label">
                    <span class="x-red">*</span>图片
                </label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="uploadPath">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
                <div class="layui-form-item">
                    <img id="imgPath" th:src="${article.imgPath}"/>
                    <input type="hidden" name="imgPath" th:value="${article.imgPath}"/>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="desc" class="layui-form-label">
                    内容
                </label>
                <div class="layui-input-block">
                    <textarea  id="desc" name="content" class="layui-textarea" th:text="${article.content}"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">保存</button>
            </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer','upload','layedit'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer,
          upload = layui.upload,
            layedit = layui.layedit;

            var desc = layedit.build('desc', {
                hideTool: ['image']
            }); //建立编辑器

          //自定义验证规则
          form.verify({

          });

            upload.render({
                elem: '#uploadPath' //绑定元素
                ,url: '/manage/article/upload' //上传接口
                ,accept:'images'
                ,done: function(res){
                    //上传完毕回调
                    $("#imgPath").attr("src",res.imgPath);
                    $("input[name='imgPath']").val(res.imgPath);
                }
                ,error: function(){
                    //请求异常回调
                }
            });

          //监听提交
          form.on('submit(add)', function(data){
              data.field.content=layedit.getContent(desc);
              $.post("/manage/article/update",data.field,function (data,status,jqhr) {
                    if(jqhr.status==200){
                        layer.alert("保存成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            parent.location.replace(parent.location.href);
                        });
                    }else{
                        layer.msg(data,{icon:2,time:1000});
                    }
              });
              return false;
          });
        });
    </script>
  </body>

</html>